/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#ifndef SX_MESH_H
#define SX_MESH_H

#include <sxGraphicsObject.h>
#include <sxArrayList.h>
#include <sxBufferUtilities.h>
#include <sxMappedSurface.h>

namespace Sx {
namespace Graphics {
namespace Objects {

/*
 * A Mesh is defined as the visual representation of an object that contains
 * a surface typically defined by the positions of several vertices as the
 * underlying physical representation. This Mesh class provides a definition
 * of the surface that is used to represent the object. The surface provides
 * the definition of its connectivity and allows for other objects (such as
 * a texture) to be mapped to it using coordinates. The surface is defined
 * using indices (each face contains a set of indices that can be used to
 * reference the physical location of the points that represent the surface).
 *
 * TLDR: A Mesh contains n, (n)-degree indexed faces. Typically used with
 * a point-based physical representation.
 */
class Mesh : public GraphicsObject {
public:
	Mesh();
	Mesh(const Mesh& mesh);
	virtual ~Mesh();

	bool construct();
	void update(float dt);

	/* Surface manipulation functions */
	bool addFace(const Primitives::IndiceFace& face);
	bool addCoordinate(const Eigen::Vector3f& coordinate);
	bool setFace(const Primitives::IndiceFace& face, unsigned int index);

	bool remove(const Primitives::IndiceFace& face);
	bool removeFace(const Primitives::IndiceFace& face);
	bool removeFace(unsigned int index);
	bool triangulateFaces();

	/* Surface information */
	unsigned int getNumFaces() const;
	Primitives::IndiceFace getFace(unsigned int index);

	/* 
	 * Surface Array Access. If a surface array is requested but this Mesh does
	 * not have a surface, it will be allocated to a default surface. A default
	 * surface does not contain any faces, normals, or coordinates.
	 */
	Util::IndexedFaceArray& getFaces();
	Util::VectorArray& getVertexNormals();
	Util::VectorArray& getFaceNormals();
	Util::VectorArray& getCoordinates();

	/* Makes a copy of the provided surface in this mesh */
	void setSurface(const MappedSurface& surface);

	/* 
	 * Returns a const pointer to the surface. The surface is owned by this
	 * Mesh object.
	 */
	const MappedSurface& getSurface() const;

protected:
	/* 
	 * A mapped surface contains a set of coordinates that can define the
	 * mapping between another object and this surface. A typical example of
	 * a mapped surface is a surface that contains a texture with texture
	 * coordinates that describe how the image is mapped to the surface.
	 *
	 * POINTER_OWNERSHIP: YES
	 */
	MappedSurface surface;
};

}

}

}

#endif
